package org.domain.pseample.session;

import java.util.List;
import java.util.Random;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import org.jboss.seam.security.Credentials;
import org.ajax4jsf.component.html.HtmlAjaxCommandLink;
import org.apache.log4j.Logger;
import java.util.ArrayList;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;

@Name("initiateConference")
public class InitiateConference {

	private static Logger logger = Logger.getLogger(InitiateConference.class);

	@In
	Credentials credentials;

	private HtmlAjaxCommandLink conference;
	private List<String> members;
	private String member;
	private static String currentMember;

	private Integer dtmf;

	public String getCurrentMember() {
		logger.info("getCurrentMember invoked");
		return currentMember;
	}

	public void setCurrentMember(String currentMember) {
		logger.info("setCurrentMember invoked " + currentMember);
		InitiateConference.currentMember = currentMember;

	}

	public String getMember() {
		return member;
	}

	public void setMember(String member) {
		this.member = member;
	}

	public List<String> getMembers() {
		return members;
	}

	public void setMembers() {
		this.members = new ArrayList<String>();
	}

	public HtmlAjaxCommandLink getConference() {
		if (conference == null)
			conference = new HtmlAjaxCommandLink();
		return conference;
	}

	public void setConference(HtmlAjaxCommandLink conference) {
		this.conference = conference;
	}

	@SuppressWarnings("deprecation")
	public void startConference() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Application application = facesContext.getApplication();
		String admin = credentials.getUsername();
		conference.setValue("End Conference");
		conference
				.setOncomplete("document.getElementById('addmembers').style.display='block'");

		conference.setAction(application.createMethodBinding(
				"#{initiateConference.endConference}", new Class[] {}));
		while (true) {
			Random randomGenerator = new Random();
			dtmf = randomGenerator.nextInt(9);

			logger.info(dtmf + "generated");

			if (ConferenceCenterManager.instance().getConferenceCenter(dtmf) == null) {
				ConferenceCenterManager.instance().setConferenceCenter(dtmf,
						admin);

				/* storing the endpoint name */
				ConferenceCenterManager
						.instance()
						.getConferenceCenter(dtmf)
						.setConferenceEndPoint("media/trunk/Conference/" + dtmf);
				ConferenceCenterManager.instance().getConferenceCenter(dtmf)
						.addConferenceMember(admin);
				members = ConferenceCenterManager.instance()
						.getConferenceCenter(dtmf).getConferenceMembersList();
				MapInstanceManager.instance().getMapInstance(admin)
						.setConferenceDtmf(dtmf);

				/* setting the user as admin */

				MapInstanceManager.instance().getMapInstance(admin)
						.setConferenceAdmin(true);
				logger.info("conference initiated");
				break;
			}
		}

	}

	@SuppressWarnings("deprecation")
	public void endConference() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ConferenceCenterManager.instance().removeConferenceCenter(dtmf);
		logger.info(conference.getOncomplete());
		Application application = facesContext.getApplication();
		logger.info("end conference");
		conference.setValue("Start Conference");
		conference.setAction(application.createMethodBinding(
				"#{initiateConference.startConference}", new Class[] {}));
		conference
				.setOncomplete("document.getElementById('addmembers').style.display='none'");
		MapInstanceManager.instance().getMapInstance(credentials.getUsername())
				.setConferenceAdmin(false);
	}

	public void addMemberToConference() {

		// logger.info(user + "added to conference");
		logger.info(member + "retrieved");

		String admin = credentials.getUsername();

		logger.info("Credentials username " + admin);

		ConferenceCenter conferenceCenter = ConferenceCenterManager.instance()
				.getConferenceCenter(
						MapInstanceManager.instance().getMapInstance(admin)
								.getConferenceDtmf());

		conferenceCenter.addConferenceMember(member);
		members = conferenceCenter.getConferenceMembersList();
		member = "";
		logger.info("change the better");
		// conferenceCenter.addConfernceUser(user);
	}

	public void removeMember() {
		logger.info(" to be removed r " + currentMember);
		String admin = credentials.getUsername();

		logger.info(InitiateConference.currentMember);
		ConferenceCenter conferenceCenter = ConferenceCenterManager.instance()
				.getConferenceCenter(
						MapInstanceManager.instance().getMapInstance(admin)
								.getConferenceDtmf());
		conferenceCenter.removeConferenceUser(currentMember);
		members = conferenceCenter.getConferenceMembersList();

	}

	public void muteMember() {
		logger.info("to be muted");
	}
}
